package org.junv;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/AddEquip")
public class AddEquip extends HttpServlet {
	private static final long serialVersionUID = 1L;
   


	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		   String x=null;
		   
		
		 //获取表单内的值。。。
		 String equipName = request.getParameter("equipName");
		 String equipCount = request.getParameter("equipCount");
		 String equipDesc = request.getParameter("equipDesc");
		 String borrable = request.getParameter("borrable");
		 String borrTime = request.getParameter("borrTime");
		 String location = request.getParameter("location");
		 String category = request.getParameter("category");
		 System.out.println("已经进入Servlet");
		
		 //控制乱码部分。。。。。。。。。。。。
		 equipName  = new String(equipName.getBytes("ISO-8859-1"),"UTF8");
		 equipCount = new String(equipCount.getBytes("ISO-8859-1"),"UTF8");
		 equipDesc = new String(equipDesc.getBytes("ISO-8859-1"),"UTF8");
		 borrable = new String(borrable.getBytes("ISO-8859-1"),"UTF8");
		 borrTime = new String(borrTime.getBytes("ISO-8859-1"),"UTF8");
		 location = new String(location.getBytes("ISO-8859-1"),"UTF8");
		 category = new String(category.getBytes("ISO-8859-1"),"UTF8");
		 //判断是否填写了内容。。。。
		 if(equipName==""){
			 
			 out.println("can't be null...器材名称不能为空！");
			 return;
		 } else{
			 out.println(equipName);
		 }
		 if(equipCount==""){
			 out.println("器材数量不能为空！");
			 return;
		 }
		 if(equipDesc==""){
			 out.println("器材描述不能为空！");
			 return;
		 }
		 if (borrable==""){
			 out.println("是否可借属性不能为空！");
			 return;
		 }else if((!borrable.equalsIgnoreCase("true"))&&(!borrable.equalsIgnoreCase("false"))){
			out.println("请输入true 或者false"); 
			return;
		 }
		 if(borrTime==""){
			 out.println("借用时间不能为空！");
			 return;
		 } if(location==""){
			 out.println("所在位置不能为空！");
			 return;
		 }
		 if(category==""){
			 System.out.println("所属类别不能为空！");
			 return;
		 }
		 
		 System.out.println("已经完成Servlet");
		 String sql = null;
		 //多么复杂的SQL语句啊。。。将器材信息加入到 equipInfo 和equipBorrAble  以及 equipToCate这三个相关表中
		 sql = "insert into equipInfo(equipName,equipCount,equipDescr) values ('"+equipName+"','"+equipCount+"','"+equipDesc+"');declare @j int;"+
               " set @j= (SELECT  equipid from equipinfo where equipName='"+equipName+"');"+
               "insert into equipBorrAble (equipId,equipBorrAble,equipBorrTime,equipBorrPlace) values (@j,'"+borrable+"','"+borrTime+"','"+location+"')"+
               "insert into equipToCate (equipId,equipCategoryId) values (@j,'"+category+"');";
		 DBBean myBean = new DBBean();
		 myBean.init();
		 boolean flag = myBean.executeUpdate(sql);
		 if(flag==true){
			 System.out.println(sql);
			 System.out.println("插入数据成功。。。。");
			 out.println("执行成功！");
		 }else{
			 out.println("执行失败！");
		 }
		 myBean.colse();
	}

	

}
